package com.spyx.sys.service.impl;

import ch.qos.logback.core.util.StringCollectionUtil;
import com.atyx.spyx.model.sys.RegionWare;
import com.atyx.spyx.vo.sys.RegionWareQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spyx.exception.SpyxException;
import com.spyx.result.ResultCodeEnum;
import com.spyx.sys.mapper.RegionWareMapper;
import com.spyx.sys.service.RegionWareService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 城市仓库关联表 服务实现类
 * </p>
 *
 * @author yhp
 * @since 2024-05-14
 */
@Service
public class RegionWareServiceImpl extends ServiceImpl<RegionWareMapper, RegionWare> implements RegionWareService {

    @Override
    public void updateRegionware(Integer id, Integer status) {
        //1.根据已知的区域id,查询区域对象
        RegionWare regionWare = baseMapper.selectById(id);
        //2.修改状态值
        regionWare.setStatus(status);
        //3.执行更新
        baseMapper.updateById(regionWare);
    }

    @Override
    public void saveRegionWare(RegionWare rw) {
        //1.校验区域是否已经开通-根据区域id,查询中间表
        LambdaQueryWrapper<RegionWare> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegionWare::getRegionId,rw.getRegionId());
        Integer count = baseMapper.selectCount(wrapper);
        //2.判断返回的条数
        if(count>0){
            throw new SpyxException(ResultCodeEnum.REGION_OPEN);
        }
        baseMapper.insert(rw);
    }

    @Override
    public IPage<RegionWare> getReginWareList(Page p1, RegionWareQueryVo queryVo) {
        //1.获取模糊查条件
        String keyword = queryVo.getKeyword();
        LambdaQueryWrapper<RegionWare> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(keyword)){
            wrapper.like(RegionWare::getRegionName,keyword)
                    .or().like(RegionWare::getWareName,keyword);
        }
        //2。执行查询
        IPage<RegionWare> pageModel=baseMapper.selectPage(p1,wrapper);

        return pageModel;
    }


}
